package com.njxzc.service.admin;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njxzc.common.http.ResponseResult;
import com.njxzc.common.security.utiles.ConfigrarionBean;
import com.njxzc.common.security.utiles.JwtTokenUtil;
import com.njxzc.common.security.utiles.RedisUtil;
import com.njxzc.entity.AUserEntity;
import com.njxzc.mapper.admin.AUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AUserServiceImpl extends ServiceImpl<AUserMapper, AUserEntity> implements AUserService {
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ConfigrarionBean config;
    // 对传入的对象参数进行标记比较
    //1.有这个用户 a.密码正确 ;b.密码不正确
    //无此用户:返回
    //2.密码正确:返回token,跳转到相应的展示页面
    @Override
    public ResponseResult<Map<String, String>> login(AUserEntity aUserEntity) {
// 获取到这个参数
        Long count = lambdaQuery().eq(AUserEntity::getAname, aUserEntity.getAname()).count();
        if(count==0) {
            return ResponseResult.getMessageResult(null, "A001");
        }
        HashMap<String, String> mapuser = new HashMap<>();
        mapuser.put("aname",aUserEntity.getAname());
        mapuser.put("apwd",aUserEntity.getApwd());
        List<AUserEntity> userlist = query().allEq(mapuser).list();
        if(!userlist.isEmpty()){
            String token = jwtTokenUtil.createToken(aUserEntity.getAname());
            redisUtil.set("login_"+aUserEntity.getAname(),aUserEntity.getAname(),config.getRedisExpiration());
            //把生成的有效信息返回给ResponseResult
            HashMap<String, String> loginresult = new HashMap<>();
            loginresult.put("authtoken",token);
            loginresult.put("aname",aUserEntity.getAname());
            loginresult.put("aid",aUserEntity.getId()+"");
            return  ResponseResult.getSuccessResult(loginresult);
        }
        else{
            return ResponseResult.getMessageResult(null,"A002");
        }
    }
}
